package com.pet.server.ext.action.home;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Campaign;
import com.pet.server.ext.data.Faxian;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.data.RequestInfo;
import com.pet.server.ext.service.ProductService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.RandomCollection;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.cache.ConstantUtil;
import com.pet.server.ext.util.cache.PetCache;
public class Index extends ActionBase{
	
	public Index(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		UserService userService = new UserService();
		
		JSONArray banner = new JSONArray();
		
        //banner
		List<Campaign> list = userService.queryForHome(BaseAttribute.pay_type.banner.toString(),6);
		for(Campaign cam:list){
			JSONObject so = new JSONObject();
			so.put("click_type", cam.click_type);
			so.put("click_url", cam.click_url);
			so.put("text", cam.text);
			so.put("img_url", cam.img_url);
			so.put("target", cam.target);
			if(!StringUtil.isEmpty(cam.param)){
				so.put("param", cam.param);	
			}
			banner.add(so);
		}
		data.put("banner", banner);
		
		
		JSONArray tuijian = new JSONArray();
		/**
		List<ProductInfo> l = PetCache.queryProductForHome(1);
		for(ProductInfo p:l){
			JSONObject so = new JSONObject();
			so.put("product_name", p.product_name);
			so.put("product_href", p.product_href);
			so.put("product_price", p.product_price);
			so.put("ico_path", p.ico_path);
			so.put("product_cate", p.product_cate);
			tuijian.add(so);
		}
        */
		List<Campaign> l = userService.queryForHome(BaseAttribute.pay_type.tuijian.toString(),3);
		
		for(Campaign p:l){
            
            
			JSONObject so = new JSONObject();
			so.put("product_name", p.text);
			so.put("product_href", p.click_url);
			so.put("product_price", p.tui_price);
			so.put("ico_path", p.img_url);
			
			if(!StringUtil.isEmpty(p.product_id)){
				ProductService productService = new ProductService();
				ProductInfo pro = productService.findProductInfoByProductId(p.product_id);
				if(pro!=null){
					so.put("client",JsonUtil.urlToJson(StringUtil.dealNull(pro.client_url,pro.product_href),pro.source));
					so.put("source", pro.source);
					so.put("client_url", pro.client_url);
					so.put("id", pro.id);
					so.put("product_href", pro.product_href);
				}
			}
			
			//so.put("product_cate", );
			tuijian.add(so);
		}

		data.put("tuijian", tuijian);
		/**
		List<Campaign> tjlist = userService.queryForHome(BaseAttribute.pay_type.tuijian.toString(),1);
		for(Campaign cam:tjlist){
			JSONObject so = new JSONObject();
			so.put("click_type", cam.click_type);
			so.put("click_url", cam.click_url);
			so.put("text", cam.text);
			so.put("img_url", cam.img_url);
			tuijian.add(so);
		}
		data.put("tuijian", banner);
       */
		
		JSONArray request = new JSONArray();
		//tiwen
		List<RequestInfo> requestList = userService.queryRequestForHome(10);
		List<RequestInfo> showList = new ArrayList<>();
		RandomCollection<RequestInfo>  random = new RandomCollection<>();
		for(RequestInfo r:requestList){
			random.add(5, r);
		}
		int i=3;
		RequestInfo req=random.removeNext();
		while(null!=req&&i>0){
			showList.add(req);
			i--;
			req=random.removeNext();
		}
		
		for(RequestInfo r:showList){
			JSONObject so = new JSONObject();
			JSONObject u = new JSONObject();
			u.put("nick_name", r.nick_name);
		    u.put("ico_path", r.ico_path);
		    u.put("user_id", r.user_id);
		    so.put("id", r.id);
		    so.put("type",r.type);
		    so.put("score", r.score);
		    so.put("user", u);
		    so.put("question", r.question);
		    so.put("cnt", r.cnt);
		    so.put("create_time",JsonUtil.getTime(r.create_time));
		    //so.put("",)
		    if(user.user_id>0){
			    so.put("sessionid",StringUtil.createSessionid(user.user_id, r.id));
		    }
		    request.add(so);
		}
	    data.put("request", request);
		
		
	    JSONArray faxian = new JSONArray();			
		List<Faxian> faxianList = PetCache.queryFaxian();
		for(Faxian fx:faxianList){
            JSONObject so = new JSONObject();
            so.put("id", fx.id);
            so.put("pet_cate", fx.pet_cate);
            so.put("breed", fx.breed);
            so.put("breed_name", ConstantUtil.getInstance().getName(fx.breed));
            so.put("bithday", JsonUtil.getTime(fx.bithday));
            so.put("description", fx.description);
            so.put("ico_path", fx.ico_path);
            so.put("sex", fx.sex);
            so.put("type", fx.type);
            faxian.add(so);
		}
		data.put("faxian", faxian);
		result.put("data", data);
		return result;
	}


}
